package it.univpm.diiga.semanticsearch.ricerca;
import it.univpm.diiga.semanticsearch.configurazione.Configuration;
import it.univpm.diiga.semanticsearch.servizi.Link;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;


public abstract class Ricerca {
	
   private String keyword;
   private int level;
   
   
   /*private List<URL> publicRepoLink=new ArrayList<URL>();
   private List<URL> defaultRepoLink=new ArrayList<URL>();*/
   
   private Hashtable<String, Object> configuration = new Hashtable<String, Object>();
      
   
   public Ricerca(){
	   //getConfiguration();
   }
 
   
   /*private void showResult(List<Link> result){
	  //mostra il risultato 
	  
   }*/
   
   public abstract List<Link> execSearch(String keyword ) throws Exception;
   
   public static boolean saveResult(List<Link> selectedResult, String path){
	   int resultSize=selectedResult.size();
	  // String path="servizi"; //deve venire dall maschera
	   Download dw=new Download();
	   URL nameURL;
	   String folderName, localFile, fileName;
	   for (int i=0; i<resultSize;i++){
		   Link ele=selectedResult.get(i);
		   fileName=ele.getServName().toLowerCase().replace(' ', '_');
		   try {
			   
			   if (ele.getServLink()!=null){
				   nameURL= new URL(ele.getServLink());
			   	   folderName=nameURL.getHost();
			   }else
				   folderName=ele.getServName().substring(0, 10).replace(' ', '_');
			   
			   
			   localFile=path+"/"+folderName+"/";
			   if (ele.getBpellLink()!=null)
				   	dw.save(ele.getBpellLink(), fileName+".bpel",localFile);
			   if (ele.getSwsalLink()!=null)
				   	dw.save(ele.getSwsalLink(), fileName+".swasl",localFile);
			   if (ele.getWsdlLink()!=null)
				   	dw.save(ele.getWsdlLink(), fileName+".wsdl",localFile);
			   
		   } catch (MalformedURLException e) {
			   e.printStackTrace();
		   }
		   //name="testing_gui";
		 
		   
			  
	   }
	   if (dw.errorReport().equals(""))
		   return true;
	   else
		   return false;
   }
   
   public String getKeyword(){
	   return this.keyword;
   }
   
   public List<URL> getDefaultRepo(){
	   //return this.defaultRepoLink;
	   return Configuration.getDefaultRepoLink();
   }
   
   public File getLocalRepo(){
	   //return this.defaultRepoLink;
	   if (!Configuration.getLocalRepoLink().equals(""))
		   return (File)Configuration.getLocalRepoLink();
	   return null;
   }
   
   public List<URL> getPublicRepo(){
	   //return this.publicRepoLink;
	   return Configuration.getPublicRepoLink();
   }
   
   public int getLevel(){
	   return this.level;
	   
   }
   
   public Hashtable getSettings(){
	   return this.configuration;
   }
   
   
   
   
   
   /*
    * ECCEZIONI
    */
  
   
   public class RemoteExc extends FileConfigExc {

		public RemoteExc() {

	    }
	    public String toString() {
	        return getMessage();
	    }
	}
   
   
   public class FileConfigExc extends Exception {
	    public FileConfigExc() {

	        super("Impossibile trovare il repository di default");
	    }
	    public String toString() {
	        return getMessage();
	    }
	}
   public class LocalExc extends FileConfigExc {
	    public LocalExc() {

	    }
	    public String toString() {
	        return getMessage();
	    }
	}
   

}
